<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <audio src="./我的神明大人.mp3"></audio>

    <button class="paused">Play</button>
    <br />
    <input type="range" min="0" max="1" step="0.01" value="1" class="volume" />

</body>

<script>
    //用于操作对象
    const AudioContext = window.AudioContext;
    const audioCtx = new AudioContext()

    const audioElement = document.querySelector('audio')
    const playBtn = document.querySelector('button')
    const volumeSlider = document.querySelector('.volume')

    const audioSource = audioCtx.createMediaElementSource(audioElement)

    playBtn.addEventListener('click', () => {
        if (audioCtx.state === 'suspended') {
            audioCtx.resume()
        }
        if(playBtn.getAttribute('class')==='paused'){
            audioElement.play()//音频播放
            playBtn.setAttribute('class','playing')
            playBtn.innerText = 'Pause'
        }else if(playBtn.getAttribute('class')==='playing'){
            audioElement.pause()
            playBtn.setAttribute('class','paused')
            playBtn.innerText = 'Play'
        }
    })

    audioElement.addEventListener('ended',()=>{
        playBtn.setAttribute('class','paused')
        playBtn.innerText = 'Play'
    })

    const gainNode = audioCtx.createGain()
    volumeSlider.addEventListener('input',()=>{
        gainNode.gain.value = volumeSlider.value
    })
    audioSource.connect(gainNode).connect(audioCtx.destination)
</script>

</html>